<?php
//
//	file: includes/attach/class_file_handler_copy.php
//	author: ptirhiik
//	begin: 05/03/2006
//	version: 1.6.2 - 09/02/2007
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	based on : attachment_mod copyright (c) 2002 Meik Sievertsen (acydburn)
//

if ( !defined('IN_PHPBB') )
{
	die('Hacking attempt');
	exit;
}

class file_handler_copy
{
	var $root_path;
	var $error;
	var $error_parm;
	var $upload_per_move;

	var $download_path;
	var $download_private;

	function file_handler_copy($ftp_root_path, $ftp_download_path)
	{
		global $config;

		// upload
		$ftp_root_path = trim(preg_replace('#(.*)?\/?$#', '\1', trim($ftp_root_path)));
		$this->root_path = $this->is_absolute($ftp_root_path) ? $ftp_root_path : $config->root . $ftp_root_path;

		// download
		$ftp_download_path = trim(preg_replace('#(.*)?\/?$#', '\1', trim($ftp_download_path)));
		$this->download_private = $this->is_absolute($ftp_download_path);
		$this->download_path = $ftp_download_path;

		// get the upload method
		$this->upload_per_move = @ini_get('safe_mode') || (@ini_get('open_basedir') && (@phpversion() >= '4.0.3'));
	}

	function check_settings($ftp_dir)
	{
		$this->error = $this->error_parm = '';
		$root_path = phpbb_realpath($this->path($ftp_dir));
		$check_file = $this->path($ftp_dir . '/0_000000.000');
		if ( empty($root_path) || !@file_exists($root_path) )
		{
			$this->error = 'Directory_does_not_exist';
			$this->error_parm = $this->root_path;
		}
		else if ( !is_dir($root_path) )
		{
			$this->error = 'Directory_is_not_a_dir';
			$this->error_parm = $this->root_path;
		}
		else if ( !($fp = @fopen($check_file, 'w')) )
		{
			$this->error = 'Directory_not_writeable';
			$this->error_parm = $this->root_path;
		}
		else
		{
			@fwrite($fp, 'test');
			@fclose($fp);
			@unlink($check_file);
		}
		return !$this->error;
	}

	function get_download_url($src_file, $with_board_root=true)
	{
		global $config;
		return $this->download_private ? false : $config->root . $this->download_path . '/' . $src_file;
	}

	function is_absolute($ftp_dir)
	{
		return !empty($ftp_dir) && (($ftp_dir[0] == '/') || (($ftp_dir[0] != '/') && ($ftp_dir[1] == ':')));
	}

	function path($ftp_dir)
	{
		$ftp_dir = empty($ftp_dir) ? '' : trim(preg_replace('#^\/?(.*?)\/?$#', '\1', $ftp_dir));
		return $this->root_path . (empty($this->root_path) || empty($ftp_dir) ? '' : '/') . $ftp_dir;
	}

	function open_channel($ftp_dir='')
	{
		$this->error = $this->error_parm = '';
		return true;
	}

	function close_channel()
	{
		return true;
	}

	function read_channel($ftp_dir, &$files, $file_prefix='')
	{
		$files = array();
		$this->error = $this->error_parm = '';

		$prefix = empty($file_prefix) ? '' : trim($file_prefix);
		$prefix_len = strlen($prefix);

		$cur_path = $this->path($ftp_dir);
		if ( $handle = @opendir($cur_path) )
		{
			while ( $filename = @readdir($handle) )
			{
				$filename = trim($filename);
				if ( !in_array($filename, array('index.php', 'index.html', '.htaccess')) && !@is_dir($cur_path . '/' . $filename) && !@is_link($cur_path . '/' . $filename) )
				{
					$filename = $prefix_len && (substr($filename, 0, $prefix_len) == $prefix) ? trim(substr($filename, $prefix_len)) : $filename;
					if ( !empty($filename) )
					{
						$files[] = $filename;
					}
				}
				@closedir($dir);
			}
		}
	}

	function upload($src_file, $mimetype, $dst_file)
	{
		// src_file & dst_file are already absolute pathes
		$this->error = $this->error_parm = '';
		if ( !is_uploaded_file($src_file) )
		{
			$this->error = 'Ftp_error_no_srcfile';
			return false;
		}
		$api_main = $this->upload_per_move ? 'move_uploaded_file' : 'copy';
		$api_else = $this->upload_per_move ? 'copy' : 'move_uploaded_file';

		if ( !@$api_main($src_file, $dst_file) && !@$api_else($src_file, $dst_file) )
		{
			$this->error = 'General_upload_error';
			$this->error_parm = basename($dst_file);
			return false;
		}
		$this->chmod($dst_file);
		return true;
	}

	// dst file is an absolute path
	function chmod($dst_file)
	{
		$dst_file = phpbb_realpath($dst_file);
		@umask(0000);
		@chmod($dst_file, 0666);
		return true;
	}

	// dst file is an absolute path
	function delete($dst_file)
	{
		$this->error = $this->error_parm = '';
		$dst_file = phpbb_realpath($dst_file);
		if ( !empty($dst_file) && !@unlink($dst_file) )
		{
			$this->error = 'General_delete_error';
			$this->error_parm = basename($dst_file);
			return false;
		}
		return true;
	}

	function get_temporary_filename($src_file='', $dst_file='')
	{
		return $dst_file;
	}

	function get_filesize($src_file)
	{
		return @filesize(phpbb_realpath($src_file));
	}

	function get_temporary($src_file, $mimetype)
	{
		return @file_exists(phpbb_realpath($src_file)) ? $src_file : false;
	}

	function unlink_temporary($tmp_file)
	{
		return true;
	}
}

?>